package service.impl;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import param.lock.Lockrequest;
import param.lock.Lockresponse;
import param.orderquery.Orderqueryrequest;
import param.orderquery.Orderqueryresponse;
import param.pub.Publicresponse;
import param.residueseatquery.Residueseatqueryrequest;
import param.residueseatquery.Residueseatqueryresponse;
import param.returnticket.Returnrequest;
import param.returnticket.Returnresponse;
import param.sell.Sellrequest;
import param.sell.Sellresponse;
import param.taketicket.Taketicketrequest;
import param.taketicket.Taketicketresponse;
import param.testcon.Testconresponse;
import param.unlock.Unlockrequest;
import param.unlock.Unlockresponse;
import service.InvokeService;
import utils.JsonUtil;
import utils.PublicXmlUtil;



public class NMGTInvokeServiceImpl implements InvokeService{


	final private Logger log = LoggerFactory.getLogger(getClass());

	@Override
	public Lockresponse lock(String serversaddress, Lockrequest obj) throws Exception {
		Lockresponse response = new Lockresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String josnrequest = null;
		String jsonString = null;
		josnrequest = JsonUtil.parseObject(obj);
		try {
			jsonString = getJsonString(serversaddress, josnrequest, "lock");
			if (jsonString == null) {
				log.error("二期锁位调用出错");
				log.error("锁位请求字符串：" + josnrequest);
				log.error("锁位返回字符串：" + jsonString);
				response.getPublicresponse().setResultcode("NALOCK1007");
				response.getPublicresponse().setResultmsg("调用车站接口失败");
				return response;
			}
		} catch (Exception e) {
			log.error("二期锁位调用出错");
			log.error("锁位请求字符串：" + josnrequest);
			log.error("锁位返回字符串：" + jsonString);
			throw e;
		}
		response = (Lockresponse) JsonUtil.readJson2Entity("{" + jsonString, Lockresponse.class);
		return response;
	}

	@Override
	public Residueseatqueryresponse residueseatquery(String serversaddress, Residueseatqueryrequest obj) throws Exception {
		Residueseatqueryresponse response = new Residueseatqueryresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String josnrequest = null;
		String jsonString = null;
		josnrequest = JsonUtil.parseObject(obj);
		try {
			jsonString = getJsonString(serversaddress, josnrequest, "residueseatquery");
			if (jsonString == null) {
				log.error("二期余票查询调用出错");
				log.error("余票查询请求字符串：" + josnrequest);
				log.error("余票查询返回字符串：" + jsonString);
				response.getPublicresponse().setResultcode("NARDQR1006");
				response.getPublicresponse().setResultmsg("调用车站接口失败");
				return response;
			}
		} catch (Exception e) {
			log.error("二期余票查询调用出错", e);
			log.error("余票查询请求字符串：" + josnrequest);
			log.error("余票查询返回字符串：" + jsonString);
			throw e;
		}
		response = (Residueseatqueryresponse) JsonUtil.readJson2Entity("{" + jsonString, Residueseatqueryresponse.class);
		return response;
	}

	@Override
	public Unlockresponse unlock(String serversaddress, Unlockrequest obj) throws Exception {
		Unlockresponse response = new Unlockresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String josnrequest = null;
		String jsonString = null;
		josnrequest = JsonUtil.parseObject(obj);
		try {
			jsonString = getJsonString(serversaddress, josnrequest, "unlock");
			if (jsonString == null) {
				log.error("二期解锁调用出错");
				log.error("解锁请求字符串：" + josnrequest);
				log.error("解锁返回字符串：" + jsonString);
				response.getPublicresponse().setResultcode("NAULCK1001");
				response.getPublicresponse().setResultmsg("调用车站接口失败");
				return response;
			}
		} catch (Exception e) {
			log.error("二期解锁调用出错", e);
			log.error("解锁请求字符串：" + josnrequest);
			log.error("解锁返回字符串：" + jsonString);
			throw e;
		}
		response = (Unlockresponse) JsonUtil.readJson2Entity("{" + jsonString, Unlockresponse.class);
		return response;
	}

	@Override
	public Sellresponse sell(String serversaddress, Sellrequest obj) throws Exception {
		Sellresponse response = new Sellresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String josnrequest = null;
		String jsonString = null;
		josnrequest = JsonUtil.parseObject(obj);
		try {
			jsonString = getJsonString(serversaddress, josnrequest, "sell");
			if (jsonString == null) {
				log.error("二期售票调用出错");
				log.error("售票请求字符串：" + josnrequest);
				log.error("售票返回字符串：" + jsonString);
				response.getPublicresponse().setResultcode("NASELL1005");
				response.getPublicresponse().setResultmsg("调用车站接口失败");
				return response;
			}
		} catch (Exception e) {
			log.error("二期售票调用出错", e);
			log.error("售票请求字符串：" + josnrequest);
			log.error("售票返回字符串：" + jsonString);
			throw e;
		}
		response = (Sellresponse) JsonUtil.readJson2Entity("{" + jsonString, Sellresponse.class);
		return response;
	}

	@Override
	public Orderqueryresponse orderquery(String serversaddress, Orderqueryrequest obj) throws Exception {
		Orderqueryresponse response = new Orderqueryresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String josnrequest = null;
		String jsonString = null;
		josnrequest = JsonUtil.parseObject(obj);
		try {
			jsonString = getJsonString(serversaddress, josnrequest, "orderquery");
			if (jsonString == null) {
				log.error("二期订单查询调用出错");
				log.error("订单查询请求字符串：" + josnrequest);
				log.error("订单查询返回字符串：" + jsonString);
				response.getPublicresponse().setResultcode("NAODQR1006");
				response.getPublicresponse().setResultmsg("调用车站接口失败");
				return response;
			}
		} catch (Exception e) {
			log.error("二期订单查询调用出错", e);
			log.error("订单查询请求字符串：" + josnrequest);
			log.error("订单查询返回字符串：" + jsonString);
			throw e;
		}
		response = (Orderqueryresponse) JsonUtil.readJson2Entity("{" + jsonString, Orderqueryresponse.class);
		return response;
	}

	@Override
	public Taketicketresponse taketicket(String serversaddress, Taketicketrequest obj) throws Exception {
		Taketicketresponse response = new Taketicketresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String josnrequest = null;
		String jsonString = null;
		josnrequest = JsonUtil.parseObject(obj);
		try {
			jsonString = getJsonString(serversaddress, josnrequest, "taketicket");
			if (jsonString == null) {
				log.error("二期取票调用出错");
				log.error("取票请求字符串：" + josnrequest);
				log.error("取票返回字符串：" + jsonString);
				response.getPublicresponse().setResultcode("NATKQR1003");
				response.getPublicresponse().setResultmsg("调用车站接口失败");
				return response;
			}
		} catch (Exception e) {
			log.error("二期取票调用出错", e);
			log.error("取票请求字符串：" + josnrequest);
			log.error("取票返回字符串：" + jsonString);
			throw e;
		}
		response = (Taketicketresponse) JsonUtil.readJson2Entity("{" + jsonString, Taketicketresponse.class);
		return response;
	}

//	@Override
//	public Changeticketnoresponse changeticketno(String serversaddress, Changeticketnorequest obj) throws Exception {
//		Changeticketnoresponse response = new Changeticketnoresponse();
//		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
//		String josnrequest = null;
//		String jsonString = null;
//		josnrequest = JsonUtil.parseObject(obj);
//		try {
//			jsonString = getJsonString(serversaddress, josnrequest, "changeticketno");
//			if (jsonString == null) {
//				log.error("二期换票号重打印调用出错");
//				log.error("换票号重打印请求字符串：" + josnrequest);
//				log.error("换票号重打印返回字符串：" + jsonString);
//				response.getPublicresponse().setResultcode("NACTKN1003");
//				response.getPublicresponse().setResultmsg("调用车站接口失败");
//				return response;
//			}
//		} catch (Exception e) {
//			log.error("二期换票号重打印调用出错", e);
//			log.error("换票号重打印请求字符串：" + josnrequest);
//			log.error("换票号重打印返回字符串：" + jsonString);
//			throw e;
//		}
//		response = (Changeticketnoresponse) JsonUtil.readJson2Entity("{" + jsonString, Changeticketnoresponse.class);
//		return response;
//	}

	@Override
	public Returnresponse returnticket(String serversaddress, Returnrequest obj) throws Exception {
		Returnresponse response = new Returnresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String josnrequest = null;
		String jsonString = null;
		josnrequest = JsonUtil.parseObject(obj);
		try {
			jsonString = getJsonString(serversaddress, josnrequest, "returnticket");
			if (jsonString == null) {
				log.error("二期退票调用出错");
				log.error("退票请求字符串：" + josnrequest);
				log.error("退票返回字符串：" + jsonString);
				response.getPublicresponse().setResultcode("NARTTK1003");
				response.getPublicresponse().setResultmsg("调用车站接口失败");
				return response;
			}
		} catch (Exception e) {
			log.error("二期退票调用出错", e);
			log.error("退票请求字符串：" + josnrequest);
			log.error("退票返回字符串：" + jsonString);
			throw e;
		}
		response = (Returnresponse) JsonUtil.readJson2Entity("{" + jsonString, Returnresponse.class);
		return response;
	}

	@Override
	public Testconresponse saletestcon(String serversaddress) {
		Testconresponse response = new Testconresponse();
		Publicresponse publicresponse = PublicXmlUtil.newPublicresponse();
		response.setPublicresponse(publicresponse);
		try {
			URL url = new URL("http://" + serversaddress);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");// 提交模式
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setConnectTimeout(6000);
			conn.setReadTimeout(6000);

			new DataOutputStream(conn.getOutputStream());
		} catch (Exception e) {
			response.getPublicresponse().setResultcode("NATEST1002");
			response.getPublicresponse().setResultmsg("业务连通性接口调用失败");
			log.error(serversaddress + "二期业务连通性接口调用失败", e);
		}
		return response;
	}

	private String getJsonString(String serversaddress, String josnrequest, String methodname) throws Exception {
		HttpURLConnection conn = null;
		try {
			URL url = new URL("http://" + serversaddress + "/" + methodname);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");// 提交模式
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setConnectTimeout(6000);
			conn.setReadTimeout(6000);

			DataOutputStream out = new DataOutputStream(conn.getOutputStream());
			// 正文，正文内容其实跟get的URL中'?'后的参数字符串一致
			String content = "jsonrequest=" + URLEncoder.encode(josnrequest, "utf-8");
			// DataOutputStream.writeBytes将字符串中的16位的 unicode字符以8位的字符形式写道流里面
			out.writeBytes(content);

			out.flush();
			out.close();

		} catch (Exception e) {
			return null;
		}

		String jsonString = null;
		try {
			BufferedReader is = new BufferedReader(new InputStreamReader(
					conn.getInputStream(),"utf-8"));
			StringBuilder sb = new StringBuilder();
			while (is.read() != -1) {
				sb.append(is.readLine());
			}
			
			jsonString = new String(sb);
			is.close();

		} catch (Exception e) {
			throw e;
			
		}
		return jsonString;
	}

	@Override
	public Testconresponse synctestcon(String serversaddress) {
		Testconresponse response = new Testconresponse();
		Publicresponse publicresponse = PublicXmlUtil.newPublicresponse();
		response.setPublicresponse(publicresponse);
		try {
			URL url = new URL("http://" + serversaddress);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");// 提交模式
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setConnectTimeout(6000);
			conn.setReadTimeout(6000);

			new DataOutputStream(conn.getOutputStream());

		} catch (Exception e) {
			response.getPublicresponse().setResultcode("NATEST1003");
			response.getPublicresponse().setResultmsg("同步连通性接口调用失败");
			log.error(serversaddress + "二期同步连通性接口调用失败", e);
		}
		return response;
	}

	@Override
	public Returnresponse specialreturnticket(String serversaddress,
			Returnrequest obj) throws Exception {
		Returnresponse response = new Returnresponse();
		response.setPublicresponse(PublicXmlUtil.newPublicresponse());
		String josnrequest = null;
		String jsonString = null;
		josnrequest = JsonUtil.parseObject(obj);
		try {
			jsonString = getJsonString(serversaddress, josnrequest, "specialreturnticket");
			if (jsonString == null) {
				log.error("二期特权退票调用出错");
				log.error("特权退票请求字符串：" + josnrequest);
				log.error("特权退票返回字符串：" + jsonString);
				response.getPublicresponse().setResultcode("NARTTK1003");
				response.getPublicresponse().setResultmsg("调用车站接口失败");
				return response;
			}
		} catch (Exception e) {
			log.error("二期特权退票调用出错", e);
			log.error("特权退票请求字符串：" + josnrequest);
			log.error("特权退票返回字符串：" + jsonString);
			throw e;
		}
		response = (Returnresponse) JsonUtil.readJson2Entity("{" + jsonString, Returnresponse.class);
		return response;
	}
}
